package com.akjava.gwt.wiki.client;


import com.akjava.wiki.client.core.DefaultHtmlDocumentConverter;
import com.akjava.wiki.client.core.DocumentBuilder;
import com.akjava.wiki.client.core.RootDocument;
import com.akjava.wiki.client.core.WikiException;
import com.akjava.wiki.client.keyword.KeyWordUtils;
import com.akjava.wiki.client.keyword.Keyword;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Main implements EntryPoint {

  private HTML htmlLabel;
private TextArea textArea;
private TextArea textHtmlArea;
private ScrollPanel htmlFolder;
/**
   * This is the entry point method.
   */
  public void onModuleLoad() {
	  editData();
     
  }
  public void editData(){

	  HorizontalPanel trueRoot=new HorizontalPanel();
	  trueRoot.setSpacing(16);
	
	  final FormPanel formPanel=new FormPanel();
      VerticalPanel verticalPanel=new VerticalPanel();
      formPanel.setWidget(verticalPanel);
      formPanel.setMethod("post");
      formPanel.setAction("exec");
      trueRoot.add(formPanel);
      
      formPanel.addFormHandler(new FormHandler() {
          public void onSubmit(FormSubmitEvent event) {
          // GWT.log(event.getSource().toString(), null);
          }

          public void onSubmitComplete(FormSubmitCompleteEvent event) {
           Window.open("Main.html","_self",null);
            //Window.alert(event.getResults());
          }
        });

      
      
      Button update = new Button("Push to Wiki2Html!", new ClickListener() {
          public void onClick(Widget sender) {
        	doWiki(); 
          }
        });

      verticalPanel.add(update);
      
   
      textArea = new TextArea();
      textArea.setName("textArea");
	  textArea.setWidth("450px");
      textArea.setHeight("200px");
      verticalPanel.add(textArea);
      
      TabPanel tp = new TabPanel();
     
      
      tp.getDeckPanel().setHeight("300px");
      tp.getDeckPanel().setWidth("450px");
     
      htmlLabel = new HTML();
      htmlFolder = new ScrollPanel(htmlLabel);
    
     
      htmlFolder.setHeight("300px");
      
	  tp.add(htmlFolder,"renderd-html");
	
     
      textHtmlArea = new TextArea();
      //textHtmlArea.setWidth("300px");
      
      tp.add(textHtmlArea,"text-html");
      tp.selectTab(0);
      verticalPanel.add(tp);
      
      verticalPanel.add(new Button("Submit", new ClickListener() {
          public void onClick(Widget sender) {
            formPanel.submit();
          }
        }));
      
     
     Frame frame=new Frame("manual_en.html");
    
     frame.setHeight("580px");
     trueRoot.add(frame);
     
      RootPanel.get().add(trueRoot);
      String data=RootPanel.get("text").getElement().getAttribute("value");
      textArea.setText(data);
  }
  
  
  
  public void doWiki(){
	  DocumentBuilder builder=new DocumentBuilder();
	  try{
   	  RootDocument document=builder.createDocument("/test.html", textArea.getText());
   	  
   	  String headerText="";
   	  String footerText="";
   	     DefaultHtmlDocumentConverter converter=new DefaultHtmlDocumentConverter();
         converter.setHeaderText(headerText);
         converter.setFooterText(footerText);
         
         converter.setIconHost("http://www.xucker.jpn.org");
         converter.setImageHost("http://www.xucker.jpn.org");
         converter.setStartImage("/img2/");
         
         
         Keyword gwt=new Keyword("GWT","http://code.google.com/webtoolkit/");
         Keyword[] keywords={gwt};
         //KeyWordUtils.insertKeyword(document,keywords,null,new String[]{""});
         String result= converter.convert(document);
         GWT.log(result, null);
        
         //htmlLabel.removeFromParent();
         htmlLabel.setHTML(result);
         
      
        // htmlFolder.add(htmlLabel);
         textHtmlArea.setText(result);
	  }catch(WikiException e){
		  GWT.log(e.getLocalizedMessage(), null);
	  }
  }
  
}
